package com.lottery.www.common.config;

import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.reflection.MetaObject;
import org.springframework.stereotype.Component;

import java.time.LocalDateTime;

/**
 * 数据库中的日期自动填充
 * 这里，要求:
 *      1.数据库中时间为create_time,类型为datetime;update_time,类型为datetime
 *      2.实体类中createTime属性，添加注解@TableField(value = "create_time", Fill=FieldFill.INSERT)
 *              updateTime,添加注解@TableField(value = "update_time", fill = FieldFill.INSERT_UPDATE)
 *
 */
@Slf4j
@Component
public class MyMetaObjectHandler implements MetaObjectHandler {
    @Override
    public void insertFill(MetaObject metaObject) {
        this.strictInsertFill(metaObject, "create_time", () -> LocalDateTime.now(), LocalDateTime.class);
    }

    @Override
    public void updateFill(MetaObject metaObject) {
        this.strictUpdateFill(metaObject, "update_time", () -> LocalDateTime.now(), LocalDateTime.class);
    }
}
